package com.example.administrator.zkdemo.db.orm.manager;

/**
 * Created with Android Studio.
 * 描述: ${DESCRIPTION}
 * Date: 2018/11/20
 * Time: 11:41
 *
 * @author 晏琦云
 * @version ${VERSION}
 */

import android.content.Context;
import android.util.Log;

import com.example.administrator.zkdemo.core.interfaces.DatabaseInterface;
import com.example.administrator.zkdemo.core.interfaces.DatabaseReceiver;
import com.example.administrator.zkdemo.db.orm.AbstractORM;
import com.zkteco.android.core.interfaces.DatabaseProvider;
import com.zkteco.android.core.library.CoreProvider;
import com.zkteco.android.db.orm.SQLiteInterfaceConnectionSource;
import com.zkteco.util.YAMLHelper;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public abstract class AbstractDataManager extends DatabaseReceiver
        implements DatabaseInterface {
    private static final int CREATETABLE_EXCEPTION = 1;
    private static final int INITDATABASE_SUCCESS = 0;
    private final String TAG = "AbstractDataManager";
    public DatabaseProvider provider;

    private void create()
            throws SQLException {
        Object localObject = getTables();
        if (localObject != null) {
            localObject = ((List) localObject).iterator();
            while (((Iterator) localObject).hasNext())
                ((AbstractORM) ((Iterator) localObject).next()).createTableWithDefaults();
        }
    }

    private void initUpdateSql(Context paramContext)
            throws IOException {
        new ArrayList();
        Object localObject = (List) YAMLHelper.getInstanceFromFile("system/config/update/SqlCommand_default.yml");
        DatabaseProvider databaseProvider = new DatabaseProvider(new CoreProvider(paramContext));
        localObject = ((List) localObject).iterator();
        while (((Iterator) localObject).hasNext()) {
            String str = (String) ((Iterator) localObject).next();
            DatabaseInterface.SQLCommand localSQLCommand = new DatabaseInterface.SQLCommand();
            if (str.substring(0, 6).equalsIgnoreCase("update")) {
                localSQLCommand.setSql(str);
                databaseProvider.executeUpdate(getDatabaseId(), localSQLCommand);
                continue;
            }
            localSQLCommand.setSql(str.substring(0, str.indexOf(";")));
            localSQLCommand.setArguments(str.substring(str.indexOf("[") + 1, str.indexOf("]")).split(","));
            databaseProvider.executeInsert(getDatabaseId(), localSQLCommand);
        }
    }

    public abstract String getDatabaseId();

    public abstract int getDatabaseVersion();

    public abstract List<AbstractORM> getTables();

    public int init(Context paramContext) {
        open(paramContext);
        SharedPreferencesManager localSharedPreferencesManager = new SharedPreferencesManager(paramContext);
        Object localObject = new StringBuilder();
        ((StringBuilder) localObject).append("isBDInitOver-");
        ((StringBuilder) localObject).append(getDatabaseId());
        localObject = localSharedPreferencesManager.get(((StringBuilder) localObject).toString(), "0");
        StringBuilder localStringBuilder = new StringBuilder();
        localStringBuilder.append("isBDInitOver-");
        localStringBuilder.append(getDatabaseId());
        localStringBuilder.append((String) localObject);
        Log.d("AbstractDataManager", localStringBuilder.toString());
        if (((String) localObject).equals("0"))
            try {
                create();
            } catch (SQLException paramContext) {
                paramContext.printStackTrace();
                return 1;
            }
        try {
            initUpdateSql(paramContext);
        } catch (IOException paramContext) {
            paramContext.printStackTrace();
        }
        paramContext = new StringBuilder();
        paramContext.append("isBDInitOver-");
        paramContext.append(getDatabaseId());
        localSharedPreferencesManager.set(paramContext.toString(), "1");
        return 0;
    }

    public void open(Context paramContext) {
        this.provider = new DatabaseProvider(new CoreProvider(paramContext));
        paramContext = new SQLiteInterfaceConnectionSource(new CoreProvider(paramContext), getDatabaseId(), getDatabaseVersion());
        try {
            AbstractORM.addSource(getDatabaseId(), paramContext);
            return;
        } catch (SQLException paramContext) {
            paramContext.printStackTrace();
        }
    }
}
